﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
//using YArchitech.Revit.MEP.Calculator;

namespace HYRElectric.SystemDiagram
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class SystemCreate : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			this.m_doc = activeUIDocument.Document;
			Result result;
			try
			{
				SystemCreateForm systemCreateForm = new SystemCreateForm(this.m_doc);
				DialogResult dialogResult;
				for (;;)
				{
					dialogResult = systemCreateForm.ShowDialog();
					if (dialogResult != DialogResult.Yes)
					{
						break;
					}
					SwitchBoxFilter switchBoxFilter = new SwitchBoxFilter();
					Reference reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, switchBoxFilter, "请选择配电箱");
					FamilyInstance familyInstance = this.m_doc.GetElement(reference) as FamilyInstance;
					List<LoopInfo> lstNewLoopInfo = new List<LoopInfo>();
					this.GetLoopInfo(familyInstance, ref lstNewLoopInfo);
					systemCreateForm.ResetLoopInfo(familyInstance.Name, lstNewLoopInfo);
				}
				if (dialogResult == DialogResult.OK)
				{
					result = new SystemDiagramDrawingControler(cmdData).Drawing(systemCreateForm.BusCalculateData, systemCreateForm.BusData, systemCreateForm.LoopCommonData, systemCreateForm.LoopData, systemCreateForm.CellInfos);
				}
				else
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			catch (Exception)
			{
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private void GetLoopInfo(FamilyInstance fi, ref List<LoopInfo> lstInfo)
		{
			if (fi == null)
			{
				return;
			}
			ElectricalSystemSet electricalSystems = fi.MEPModel.ElectricalSystems;
			if (electricalSystems == null)
			{
				return;
			}
			ElectricalSystemSetIterator electricalSystemSetIterator = electricalSystems.ForwardIterator();
			while (electricalSystemSetIterator.MoveNext())
			{
				object obj = electricalSystemSetIterator.Current;
				ElectricalSystem electricalSystem = obj as ElectricalSystem;
				string name = electricalSystem.Name;
				double dA = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEA).AsDouble();
				double dB = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEB).AsDouble();
				double dC = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEC).AsDouble();
				string phaseSequence = this.GetPhaseSequence(dA, dB, dC);
				double num = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD).AsDouble();
				num = UnitConvert.CovertFromAPI((DisplayUnitType)84, num) / 1000.0;
				double num2 = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_POWER_FACTOR).AsDouble();
				string text = electricalSystem.get_Parameter(BuiltInParameter.CIRCUIT_LOAD_CLASSIFICATION_PARAM).AsString();
				if (text == "备用")
				{
					num = 0.0;
				}
				double coefficient = this.GetCoefficient(text);
				double electricCurrent = this.GetElectricCurrent(phaseSequence, num, coefficient, num2);
				List<string> list = new List<string>();
				list.Add("断路器");
				list.Add("空");
				list.Add("空");
				List<string> list2 = new List<string>();
				list2.Add("");
				list2.Add("");
				list2.Add("");
				LoopInfo item = new LoopInfo(electricalSystem.Name, phaseSequence, num, coefficient, num2, text, electricCurrent, list, list2, 0.0, 0.0);
				lstInfo.Add(item);
			}
		}

		private string GetPhaseSequence(double dA, double dB, double dC)
		{
			List<string> list = new List<string>();
			if (!dA.Equals(0.0))
			{
				list.Add("L1");
			}
			if (!dB.Equals(0.0))
			{
				list.Add("L2");
			}
			if (!dC.Equals(0.0))
			{
				list.Add("L3");
			}
			if (list.Count <= 0)
			{
				return "单相";
			}
			if (list.Count >= 2)
			{
				return "三相";
			}
			return list[0];
		}

		private double GetCoefficient(string strName)
		{
			double result = 1.0;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(ElectricalLoadClassification));
			if (filteredElementCollector != null)
			{
				ElectricalLoadClassification electricalLoadClassification = null;
				foreach (Element element in filteredElementCollector.ToElements())
				{
					if (element.Name == strName)
					{
						electricalLoadClassification = (element as ElectricalLoadClassification);
						break;
					}
				}
				foreach (ElectricalDemandFactorValue electricalDemandFactorValue in (this.m_doc.GetElement(electricalLoadClassification.DemandFactorId) as ElectricalDemandFactorDefinition).GetValues())
				{
					result = electricalDemandFactorValue.Factor;
				}
			}
			return result;
		}

		private double GetElectricCurrent(string strPhase, double dLoad, double dCoefficient, double dPower)
		{
            //double activePower = DataCalculationAndHelper.ActivePower(dLoad, dCoefficient);
            //double reactivePower = DataCalculationAndHelper.RectivePower(activePower, dPower);
            //double viewPower = DataCalculationAndHelper.ViewPower(activePower, reactivePower);
			double value = 0;
            //if (strPhase == "三相")
            //{
            //    value = DataCalculationAndHelper.EleCurrentThree(viewPower);
            //}
            //else
            //{
            //    value = DataCalculationAndHelper.EleCurrentOne(viewPower);
            //}
			return Math.Round(value, 2);
		}

		private new Document m_doc;
	}
}
